package com.example.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.VideoView;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    private static final String TAG = "SplashActivity";
    private VideoView videoView;
    private Button btnSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        videoView = findViewById(R.id.videoView);
        btnSkip = findViewById(R.id.btnSkip);

        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash_video);
        videoView.setVideoURI(videoUri);

        videoView.setOnPreparedListener(mp -> {
            Log.d(TAG, "视频准备就绪");
            mp.setLooping(false); // 不循环播放

            new Handler().postDelayed(() -> {
                btnSkip.setVisibility(View.VISIBLE);
            }, 3000);

            videoView.start();
        });

        videoView.setOnCompletionListener(mp -> {
            Log.d(TAG, "视频播放完成");
            startMainActivity();
        });

        btnSkip.setOnClickListener(v -> {
            Log.d(TAG, "用户点击跳过");
            startMainActivity();
        });
    }

    private void startMainActivity() {
        Log.d(TAG, "启动MainActivity");
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "SplashActivity销毁");
        if (videoView != null) {
            videoView.stopPlayback();
        }
    }
}